package com.jinke.api.modules.app.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinke.api.common.api.CommonPage;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.enums.QuestionType;
import com.jinke.api.modules.app.enums.ReplyType;
import com.jinke.api.modules.app.enums.Roles;
import com.jinke.api.modules.app.model.UserQuestion;
import com.jinke.api.modules.app.request.QuestionDetailRequest;
import com.jinke.api.modules.app.request.QuestionQueryRequest;
import com.jinke.api.modules.app.request.QuestionReplyRequest;
import com.jinke.api.modules.app.response.UploadResponse;
import com.jinke.api.modules.app.response.UserQuestionVO;
import com.jinke.api.modules.app.service.SysMessageService;
import com.jinke.api.modules.app.service.UserQuestionService;
import com.jinke.api.modules.ums.model.UmsAdmin;
import com.jinke.api.modules.ums.model.UmsRole;
import com.jinke.api.modules.ums.service.UmsAdminService;
import com.jinke.api.modules.ums.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/app/user/question")
@Api(value = "用户工单管理", tags = "用户工单管理")
public class UserQuestionController {
    @Autowired
    private SysMessageService sysMessageService;
    @Resource
    UserQuestionService userQuestionService;
    @Resource
    private UmsAdminService umsAdminService;
    @ApiOperation("提交工单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody UserQuestion userQuestion) {
        String descFromQuestionType = QuestionType.getDescFromValue(userQuestion.getType().getValue());
        System.out.println(descFromQuestionType);
        UmsAdmin userInfo = UserUtil.getUserInfo();

        String htmlContent = "<body><p>来自<"+
                userInfo.getUsername()+" "
                + userInfo.getRealName()+" " +userInfo.getEmail()+ ">的消息</p><br/><p>主题: "
                + userQuestion.getTitle() + "</p><br/><p>工单类型: "
                +descFromQuestionType + "</p><br/>我的要求: "
                + userQuestion.getContent()
                + "</p><br/></body>";

        try {
            String email=null;
            if (descFromQuestionType.contains("财务")){
                email="tujing@ship-cheap.com";
            }
            sysMessageService.sendMailToUs(htmlContent, userQuestion.getAttachments(),"工单",email);
        }catch  (Exception e){
            e.printStackTrace();
        }
        return userQuestionService.create(userQuestion);
    }

    @ApiOperation("工单回复")
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult reply(@RequestBody QuestionReplyRequest questionReplyRequest) {
        questionReplyRequest.setUserId(UserUtil.getUserInfo().getId());
        return userQuestionService.reply(questionReplyRequest);
    }

    @ApiOperation("根据ID获取工单详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UserQuestionVO> getItem(QuestionDetailRequest questionDetailRequest) {
        questionDetailRequest.setUserId(UserUtil.getUserInfo().getId());
        UserQuestionVO detail = userQuestionService.detail(questionDetailRequest);
        return CommonResult.success(detail);
    }

    @ApiOperation("关闭")
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<UserQuestionVO> close(@RequestBody QuestionDetailRequest questionDetailRequest) {
        questionDetailRequest.setUserId(UserUtil.getUserInfo().getId());
        return userQuestionService.close(questionDetailRequest);
    }

    @ApiOperation("重新打开")
    @RequestMapping(value = "/reopen", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<UserQuestionVO> reopen(@RequestBody QuestionReplyRequest questionReplyRequest) {
        questionReplyRequest.setUserId(UserUtil.getUserInfo().getId());
        return userQuestionService.reopen(questionReplyRequest);
    }

    @ApiOperation("工单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<UserQuestionVO>> list(QuestionQueryRequest request) {
        request.setUserId(UserUtil.getUserInfo().getId());
        Page<UserQuestionVO> list = userQuestionService.list(request);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("上传图片")
    @PostMapping(value = "/upload")
    @ResponseBody
    public CommonResult<UploadResponse> UploadImage(@RequestParam("file") MultipartFile file, Integer id, ReplyType replyType) {
        return userQuestionService.upload(file, id, replyType);

    }

    @ApiOperation("是否有新消息")
    @RequestMapping(value = "/unread", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Long> unread() {
        QuestionDetailRequest questionDetailRequest = new QuestionDetailRequest();

        UmsAdmin userInfo = UserUtil.getUserInfo();
        Integer id = userInfo.getId();
        List<UmsRole> roleList = umsAdminService.getRoleList(id);
        List<Integer> roles = roleList.stream().map(x -> {
            return x.getId();
        }).collect(Collectors.toList());
        if(roles.contains(Roles.USER.getValue())){
            questionDetailRequest.setUserId(id);
        }
        return userQuestionService.unread(questionDetailRequest);
    }

}

